package com.action.wx.msg;

import com.action.pc.user.JsVo;
import com.action.wx.common.CommonAction;
import com.bean.Member;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.service.member.MemberService;
import com.service.msg.MsgService;
import com.util.MathUtil;
import com.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/9/11.
 */
@Controller
@RequestMapping("wx/msg")
public class MsgAction extends BaseAction {
    Logger log = Logger.getLogger(MsgAction.class);
    @Autowired
    private MemberService memberService;

    @Autowired
    private MsgService msgService;

    @ResponseBody
    @RequestMapping("sendCode")
    public String sendCode(ModelAndJson modelMap, String tel,Integer mid,String type) {
        try {
            if(!StringUtil.isMobile(tel)){
                modelMap.setFalse("error","手机号格式有误");
                return modelMap.toString();
            }
            Member member = memberService.selectByPrimaryKey(mid);
            if(member==null){
                modelMap.setFalse("error","会员出现异常");
                return modelMap.toString();
            }
            if(StringUtil.isNotEmpty(type) && "bind_merchant".equals(type)){
                //如果type=bind_merchant 不验证手机号是否注册
            }else{
                member=memberService.getMeberByTel(tel);
                if(member!=null){
                    modelMap.setFalse("error","该手机号已经注册");
                    return modelMap.toString();
                }
            }
            msgService.sendMsg(tel);
            modelMap.setTrue("发送成功");
        }catch (Exception e){
            modelMap.setFalse("error","短信发送出现异常");
            return modelMap.toString();
        }
        return modelMap.toString();
    }

    @ResponseBody
    @RequestMapping("checkCode")
    public String checkCode(ModelAndJson modelMap, String tel,String code) {
        try {
            if(StringUtils.isBlank(code)){
                modelMap.setFalse("error","请输入验证码");
                return modelMap.toString();
            }
            boolean result = msgService.checkCode(tel, code);
            if(result){
                modelMap.setTrue("校验成功");
            }else{
                modelMap.setFalse("error","验证码不正确");
            }
        }catch (Exception e){
            modelMap.setFalse("error","校验出现异常");
            return modelMap.toString();
        }
        return modelMap.toString();
    }

}
